package com.example.binjiangapp.controller;

import com.example.binjiangapp.DAO.Dto.UserR;
import com.example.binjiangapp.common.AjaxResult;
import com.example.binjiangapp.common.utils.JwtUtils;
import com.example.binjiangapp.service.impl.loginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/satellite")
@RestController
public class loginController {
    @Autowired
    loginServiceImpl loginService;

    @GetMapping("/index")
    public AjaxResult index(HttpServletRequest request) {

        String username = (String) request.getAttribute("username");
        String age = (String) request.getAttribute("password");
        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        return AjaxResult.success(map);
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserR username) {
        // 判断数据库是否有该用户
        if(loginService.hasUser(username)){
            Map<String, String> res = new HashMap<>();
            Map<String, String> map = new HashMap<>();
            map.put("username", username.getUsername());
            map.put("password", username.getPassword());
            String token = JwtUtils.createToken(map);
            res.put("token",token);
            return AjaxResult.success("登录成功",res);
        }else{
            return AjaxResult.error("账号密码错误");
        }
    }
}
